<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <!-- logo区域 -->
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <!-- 表单区域 -->

        <el-form ref="form" :model="form" :rules="rules" class="login-form">
        <el-form-item label="账号" prop="mobile">
            <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="code">
            <el-input v-model="form.code" show-password></el-input>
        </el-form-item>
        <el-form-item prop="isAgree">
            <el-checkbox v-model="form.isAgree" label="我已阅读并同意用户协议和隐私条款" name="type"></el-checkbox>
        </el-form-item>
        <el-form-item>
            <el-button v-loading.fullscreen.lock="form.loading" element-loading-text="拼命加载中" @click.prevent="hLogin">登录</el-button>
            <el-button>取消</el-button>
        </el-form-item>
        </el-form>
    </div>
  </div>
</template>
<style scoped lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("http://fanyoufu2.gitee.io/hmpc/img/login_bg.ec9bf219.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("http://fanyoufu2.gitee.io/hmpc/img/logo_index.e0892259.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
<script>
// import axios from 'axios'
export default {
  data () {
    return {
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[345678]\d{9}$/, message: '请输入合法的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '请输入合法的验证码', trigger: 'blur' }
        ],
        isAgree: [
          {
            validator: function (rule, value, callback) {
              console.log(rule, value, callback)
              if (value) {
                callback()
              } else {
                callback(new Error('必须同意条款才能登录'))
              }
            },
            trigger: 'change'
          }
        ]
      },
      form: {
        mobile: '13911111111',
        code: '246810',
        isAgree: true,
        loading: false
      }
    }
  },
  methods: {
    async doLogin () {
      try {
        const { mobile, code } = this.form
        this.form.loading = true
        const res = await this.$axios({
          url: '/mp/v1_0/authorizations',
          method: 'POST',
          data: {
            mobile,
            code
          }
        })
        this.$message({
          message: '登录成功',
          type: 'success'
        })
        this.form.loading = false
        localStorage.setItem('myStr', res.data.data.token)
        this.$router.push('/')
      } catch {
        this.$message({
          message: '失败'
        })
        this.form.loading = false
      }
    },
    hLogin () {
    // console.log(this.$refs.form)
      this.$refs.form.validate(valid => {
        // valid 就是表单验证的结果，如果是true，表示通过了
        // console.log(valid)
        if (valid) {
          this.doLogin()
        }
      })
    }
  }
}
</script>
